package com.shop.bookstore.service;
import com.shop.bookstore.dao.UserDao;
import com.shop.bookstore.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
@Service

public class UserService implements UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int add(User user) {
        String sql="insert into user values(null,'"+user.getUsername()
                +"','"+user.getPassword()+"','0','0')";
        System.out.println(sql);
        return jdbcTemplate.update(sql);
    }

    @Override
    public int delete(User user) {
        return 0;
    }

    @Override
    public int update(User user) {
        return 0;
    }
    @Override
    public int save(User user) {
        String sql = "INSERT INTO user(username, password) VALUES(?, ?)";
        Object[] params = {user.getUsername(), user.getPassword()};
        return jdbcTemplate.update(sql, params);
    }

    @Override
    public User findOne(User user) {
        String sql="select * from user where username like '%"+user.getUsername()+"%'";

        System.out.println(sql);
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class));
    }

    @Override
    public List<User> findAll(User user) {
        return null;
    }
}